import java.util.HashMap;
import java.util.Map;

/**
 * @Author ZhangCuirong
 * @Date 2025/9/16 16:35
 * @description:
 */
public class Solution13 {
    public int romanToInt(String s) {
        Map<String, Integer> map = new HashMap<>();
        map.put("I", 1);
        map.put("IV", 4);
        map.put("V", 5);
        map.put("IX", 9);
        map.put("X", 10);
        map.put("XL", 40);
        map.put("L", 50);
        map.put("XC", 90);
        map.put("C", 100);
        map.put("CD", 400);
        map.put("D", 500);
        map.put("CM", 900);
        map.put("M", 1000);
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (i + 1 < s.length()) {
                String next = s.substring(i, i + 2);
                if (map.containsKey(next)) {
                    res += map.get((next));
                    i++;
                    continue;
                }
            }
            res += map.get((String.valueOf(c)));
        }
        return res;
    }

    public static void main(String[] args) {
        Solution13 solution = new Solution13();
        System.out.println(solution.romanToInt("III"));
        System.out.println(solution.romanToInt("IV"));
        System.out.println(solution.romanToInt("IX"));
        System.out.println(solution.romanToInt("LVIII"));
        System.out.println(solution.romanToInt("MCMXCIV"));
    }
}
